package lc.二叉树;

import utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/11
 */
public class Math222_完全二叉树的节点个数 {

    public int countNodes(TreeNode root) {

        if (root == null) {
            return 0;
        }
        int leftNum = countNodes(root.left);
        int rightNum = countNodes(root.right);

        return leftNum + rightNum + 1;
    }

    public int countNodes1(TreeNode root) {

        if (root == null) {
            return 0;
        }
        int res = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                res++;
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }

        }
        return res;
    }
}
